// -----------------------------------------------------------
// Game Framework
// Copyright © 2024-2024 deadcycle. All rights reserved.
// Homepage: https://deadcycle.gitee.io
// Feedback: deadcycle@163.com
// -----------------------------------------------------------

#if ODIN_INSPECTOR

using System;
using System.Collections.Generic;
using System.Reflection;

using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;

using UnityEditor;

using UnityEngine;

namespace UIWidget.Editor
{
    [CustomEditor(typeof(LayoutGroupBase))]
    public class LayoutGroupBaseInspector : OdinEditor
    {
        private GUIStyle m_NoticeStyle;

        protected override void OnEnable()
        {
            base.OnEnable();

            m_NoticeStyle = new GUIStyle
            {
                wordWrap = true, fontSize = 14, normal = { textColor = Color.magenta },
            };

            LayoutGroupBase layoutGroupBase = target as LayoutGroupBase;
            layoutGroupBase.ForceRebuildLayout();
        }

        public override void OnInspectorGUI()
        {
            EditorGUILayout.LabelField("注意: 操作子节点元素的显示隐藏, 当前Inspector未激活时, 无法触发布局组自动刷新",
                m_NoticeStyle);

            EditorGUILayout.Space(10);
            base.OnInspectorGUI();
        }
    }

    public class LayoutGroupBaseAttrProcessor : OdinAttributeProcessor<LayoutGroupBase>
    {
        private readonly Dictionary<string, Action<List<Attribute>>> m_FieldProcessorDict;
        private readonly Dictionary<string, Action<List<Attribute>>> m_MethodProcessorDict;

        public LayoutGroupBaseAttrProcessor()
        {
            m_FieldProcessorDict = new Dictionary<string, Action<List<Attribute>>>
            {
                { "m_ChildAlignment", FieldChildAlignment },
                { "m_DisallowChildInstance", FieldDisallowChildInstance },
            };

            m_MethodProcessorDict = new Dictionary<string, Action<List<Attribute>>>
            {
                { "ForceRebuildLayout", MethodForceRebuildLayout },
            };
        }

        public override void ProcessChildMemberAttributes(InspectorProperty parentProperty,
            MemberInfo member,
            List<Attribute> attributes)
        {
            if (member.MemberType == MemberTypes.Field &&
                m_FieldProcessorDict.TryGetValue(member.Name,
                    out Action<List<Attribute>> fieldProcessor))
            {
                fieldProcessor.Invoke(attributes);
            }
            else if (member.MemberType == MemberTypes.Method &&
                m_MethodProcessorDict.TryGetValue(member.Name,
                    out Action<List<Attribute>> methodProcessor))
            {
                methodProcessor.Invoke(attributes);
            }
        }

        private void FieldChildAlignment(List<Attribute> attributes)
        {
            attributes.Add(new LabelTextAttribute("子元素对齐方式"));
            attributes.Add(new OnValueChangedAttribute("ForceRebuildLayout"));
        }

        private void FieldDisallowChildInstance(List<Attribute> attributes)
        {
            attributes.Add(new LabelTextAttribute("运行时禁止新增元素"));
        }

        private void MethodForceRebuildLayout(List<Attribute> attributes)
        {
            attributes.Add(new ButtonAttribute("强制重构布局"));
        }
    }
}

#endif